package org.xu.controller;

import com.core.data.Reply;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xu.logic.FileLogic;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

@Slf4j
@RequestMapping("/file/")
@Controller
public class FileController {

    private static final String fileNameRegex = "[&#=/?+%;:@-]";

    @Autowired
    private FileLogic fileLogic;

    @ResponseBody
    @RequestMapping("/showUrl")
    public Reply<String> showUrl(@RequestParam("url") String url){
        return fileLogic.showUrl(url);
    }


    @RequestMapping("/downLoad")
    public void downLoad(HttpServletRequest request, HttpServletResponse response){
        log.info("------");
        fileLogic.downLoad(request,response);
    }

    public static void main(String[] args) {
        File zipFile = new File("C:\\Users\\liuxiaoxu\\Downloads\\批次号_ACS202407110002.zip");
        if (validateZipFile(zipFile)) {
            System.out.println("ZIP 文件结构正确。");
        } else {
            System.out.println("ZIP 文件结构不正确或存在问题。");
        }
    }

    public static boolean validateZipFile(File zipFile) {
        try (FileInputStream fis = new FileInputStream(zipFile);
             ZipInputStream zis = new ZipInputStream(fis)) {

            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                if (entry.isDirectory()) {
                    System.out.println("目录: " + entry.getName());
                } else {
                    System.out.println("文件: " + entry.getName());
                }
                // 读取文件内容以确保文件未损坏
                byte[] buffer = new byte[1024];
                while (zis.read(buffer) != -1) {
                    // 读取文件内容
                }
                zis.closeEntry();
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

}
